package dao.caminhoneiro;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import entidade.caminhoneiro.Caminhoneiro;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
import apresentacao.caminhoneiro.Activity_Menu_Principal;

// Abre uma nova thread para nao deixar o aplicativo pesado e faz o log-in
public class AsyncTask_Logar extends AsyncTask<Void, Void, String>{
	private ProgressDialog progress;
	Context context;
	String CPF, senha;
	
	private List<NameValuePair> param;
	
	private static final String SERVER = "http://nome-produtor.rhcloud.com/caminhoneiro_id/";
	
	public AsyncTask_Logar(String CPF, String senha, Context context) {
		this.context = context;
		this.CPF = CPF;
		this.senha = senha;
	}
	
	@Override
	protected String doInBackground(Void... params) {
			param = new ArrayList<NameValuePair>();
			param.add(new BasicNameValuePair("CPF", 		CPF));
			param.add(new BasicNameValuePair("senha", 		senha));
			String line = null;
			try {
			line = doPostRequest(SERVER).readLine();	
			if(!line.equals("None")){
				String[] fl = line.split(",");
				Activity_Menu_Principal.user_id = Integer.valueOf(fl[0]);
				Activity_Menu_Principal.isNotifyOn = false;
				return "OK";
			}
			}catch (IOException e) {
				e.printStackTrace();
			}
			return line;
	}
	
	private BufferedReader doPostRequest(String server) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(server);
		
	    try {
			post.setEntity(new UrlEncodedFormEntity(param));
			HttpResponse response = client.execute(post);
			return new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return null;
	}
	
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		progress.dismiss();
		if(result.equals("OK")){
			Intent it = new Intent(context, Activity_Menu_Principal.class);
			context.startActivity(it);
			
		}else{
			Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
			Toast.makeText(context, "Erro ao logar.", Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		progress = ProgressDialog.show(context, "Caminhoneiro", "Entrando no sistema, aguarde");
	}
}
